<?php


namespace app\controller\admin;


use app\model\cart\VerifyCartComplaintModel;
use app\model\coach\UserCoachModel;
use app\model\partner\UserPartnerModel;
use app\model\user\UserEvaluateModel;

class ComplaintController extends BaseAdminController
{

    function setModel()
    {
        return new VerifyCartComplaintModel();
    }

    function setWithModel()
    {
        return ['user'];
    }

    function setSearch()
    {
        $where = [];
        $type = $this->request->param('type', '');
        $name = $this->request->param('coach_name', '', 'trim');
        if ($type != '') {
            $where [] = ['type', '=', $type];
        }
        if ($name != '') {
            if ($type == 0) {
                $coachIdArr = UserPartnerModel::where('username', 'like', "%{$name}%")->column('id');
            } elseif ($type == 1) {
                $coachIdArr = UserCoachModel::where('username', 'like', "%{$name}%")->column('id');
            } else {
                $coachIdArr1 = UserPartnerModel::where('username', 'like', "%{$name}%")->column('id');
                $coachIdArr2 = UserCoachModel::where('username', 'like', "%{$name}%")->column('id');
                $coachIdArr = array_merge($coachIdArr1, $coachIdArr2);
            }
            $where [] = ['coach_user_id', 'in', $coachIdArr];
        }
        return [
            '=' => ['status'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    public function getList()
    {
        try {
            $list = parent::getList()->each(function ($item) {
                if ($item['type'] == 0) {
                    $item['coach_name'] = UserPartnerModel::where('id', $item['coach_user_id'])->value('username');
                } else {
                    $item['coach_name'] = UserCoachModel::where('id', $item['coach_user_id'])->value('username');
                }
            });
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }

    }
}